/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.alarm.overview;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;

import twaver.Alarm;
import twaver.TWaverUtil;
 
public class AlarmUI extends JPanel {
    BorderLayout layout = new BorderLayout();
    JPanel centerPane = TWaverUtil.createVerticalPanel(5);
    AlarmUIBasicPane basicPane = new AlarmUIBasicPane();
    AlarmUITimePane timePane = new AlarmUITimePane();
    AlarmUITextPane textPane = new AlarmUITextPane();
    AlarmUIListPane listPane = new AlarmUIListPane();
    JPanel southPane = new JPanel();
    BorderLayout southPaneLayout = new BorderLayout();

    private Alarm alarm = null;

    public AlarmUI(Alarm alarm) {
        init();
        setAlarm(alarm);
    }

    public void setAlarm(Alarm alarm) {
        this.alarm = alarm;
        this.basicPane.setAlarm(alarm);
        this.timePane.setAlarm(alarm);
        this.textPane.setAlarm(alarm);
        this.listPane.setAlarm(alarm);

        //find the bigest width label.
        int max = 0;
        for (int i = 0; i < 3; i++) {
            JPanel p = null;
            if (i == 0) {
                p = this.basicPane;
            }
            if (i == 1) {
                p = this.timePane;
            }
            if (i == 2) {
                p = this.textPane;
            }
            for (int j = 0; j < p.getComponentCount(); j++) {
                Component c = p.getComponent(j);
                if (c instanceof JLabel) {
                    if (c.getPreferredSize().width > max) {
                        max = c.getPreferredSize().width;
                    }
                }
            }
        }
        //use the max width for everybody.
        for (int i = 0; i < 3; i++) {
            JPanel p = null;
            if (i == 0) {
                p = this.basicPane;
            }
            if (i == 1) {
                p = this.timePane;
            }
            if (i == 2) {
                p = this.textPane;
            }
            for (int j = 0; j < p.getComponentCount(); j++) {
                Component c = p.getComponent(j);
                if (c instanceof JLabel) {
                    JLabel label = (JLabel) c;
                    int x = max;
                    int y = label.getPreferredSize().height;
                    label.setPreferredSize(new Dimension(x, y));
                }
            }
        }
    }

    public Alarm getAlarm() {
        return alarm;
    }

    private void init() {
        this.setLayout(layout);
        centerPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 0, 10));
        southPane.setLayout(southPaneLayout);
        southPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
        this.add(centerPane, BorderLayout.NORTH);
        this.add(southPane, BorderLayout.CENTER);
        southPane.add(listPane, BorderLayout.CENTER);
        centerPane.add(basicPane);
        centerPane.add(new JSeparator());
        centerPane.add(timePane);
        centerPane.add(new JSeparator());
        centerPane.add(textPane);
        centerPane.add(new JSeparator());
    }

    public static JFrame createFrameAlarmUI(Alarm alarm) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        if (alarm != null) {
            frame.setTitle("Alarm '" +
                           alarm.getAlarmID().toString() +
                           "' Properties");
        } else {
            frame.setTitle("Alarm Properties");
        }
        AlarmUI ui = new AlarmUI(alarm);
        frame.setContentPane(ui);
        frame.setSize(550, 600);
        TWaverUtil.centerWindow(frame);

        return frame;
    }

    public static JDialog createDialogAlarmUI(Alarm alarm) {
        JDialog dialog = new JDialog();
        dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        if (alarm != null) {
            dialog.setTitle("Alarm '" +
                            alarm.getAlarmID().toString() +
                            "' Properties");
        } else {
            dialog.setTitle("Alarm Properties");
        }
        AlarmUI ui = new AlarmUI(alarm);
        dialog.setContentPane(ui);
        dialog.setSize(550, 600);
        TWaverUtil.centerWindow(dialog);

        return dialog;
    }

}